package com.p2p.front.dal.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.p2p.base.dao.BaseMapper;
import com.p2p.base.page.PageVo;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.web.form.LoanForm;

public interface LoanInfoDao extends BaseMapper{
	
	String columns = "lid,plid,uid,aid,title,img,type,is_flow,is_field,is_plan,is_okout,is_newbie,no_captcha,use_type,amount,late_fee,early_fee,fee_rate,mfee_rate,field_fee,credit_fee,other_fee,tender_min,tender_max,tender_limit_times,tender_count,tender_begin,tender_end,deadline,deadline_type,days,apr,reward_apr,reward_money,repay_method,progress,exptime,begintime,endtime,status,ostatus,auto,description,remark,lrealname,lidcard,lno,addtime,uptime,paytime";
	@Select("select lid,plid,uid,aid,title,img,type,is_flow,is_field,is_plan,is_okout,is_newbie,no_captcha,use_type,amount,late_fee,early_fee,fee_rate,mfee_rate,field_fee,credit_fee,other_fee,tender_min,tender_max,tender_limit_times,tender_count,tender_begin,tender_end,deadline,deadline_type,days,apr,reward_apr,reward_money,repay_method,progress,exptime,begintime,endtime,status,ostatus,auto,description,remark,lrealname,lidcard,lno,addtime,uptime,paytime from p2p_loan_info where status >=21 order by lid desc")
	List<LoanInfo> listPage(LoanForm form);
	
	List<LoanInfo> listPageBackend(PageVo form);
	
	@Select("select lid,plid,uid,aid,title,img,type,is_flow,is_field,is_plan,is_okout,is_newbie,no_captcha,use_type,amount,late_fee,early_fee,fee_rate,mfee_rate,field_fee,credit_fee,other_fee,tender_min,tender_max,tender_limit_times,tender_count,tender_begin,tender_end,deadline,deadline_type,days,apr,reward_apr,reward_money,repay_method,progress,exptime,begintime,endtime,status,ostatus,auto,description,remark,lrealname,lidcard,lno,addtime,uptime,paytime from p2p_loan_info where status = #{status} order by lid desc")
	List<LoanInfo> listPageByStatus(LoanForm form);

	@Select("select "+columns+" from p2p_loan_info where lid=#{id}")
	@ResultMap(value="LoanInfoResultMap")
	LoanInfo getLoanInfoById(Long id);
	
	@Select("select "+columns+" from p2p_loan_info where lid=#{id}")
	Map<String, Object> get(Long id);
	
	@Select("select * from p2p_loan_info where status in (12,21,32,41,51) order by tender_begin desc limit 0,#{count}")
	@ResultMap(value="LoanInfoResultMap")
	List<LoanInfo> list(Integer count);
	
	List<Map<String, Object>> batchList(List list);
	
	List<LoanInfo> findByIdList(List<Long> lidList);
	
	@Update("update p2p_loan_info set ostatus = #{ostatus} ,status=#{status} where lid=#{lid}")
	int updateStatus(@Param(value="lid")Long lid,@Param(value="status")int status,@Param(value="ostatus")int ostatus);
	
	@Insert("insert into p2p_loan_info(uid,title,type,is_flow,is_field,is_plan,is_okout,is_newbie,no_captcha,"
			+ "use_type,amount,late_fee,early_fee,fee_rate,mfee_rate,field_fee,credit_fee,other_fee,repay_method,"
			+ "tender_min,tender_max,tender_limit_times,tender_count,tender_begin,tender_end,deadline,"
			+ "deadline_type,apr,credit_rate,reward_apr,status,lrealname,lidcard,lno,remark,description,exptime,addtime) values(#{uid},"
			+ "#{title},#{type},#{isFlow},#{isField},#{isPlan},"
			+ "#{isOkout},#{isNewbie},#{noCaptcha},#{useType},#{amount.cent},#{lateFee.cent},"
			+ "#{earlyFee.cent},#{feeRate},#{mfeeRate},#{fieldFee.cent},#{creditFee.cent},#{otherFee.cent},#{repayMethod},"
			+ "#{tenderMin.cent},#{tenderMax.cent},#{tenderLimitTimes},#{tenderCount},#{tenderBegin},"
			+ "#{tenderEnd},#{deadline},#{deadlineType},#{apr},#{creditRate},#{rewardApr},#{status},#{lrealname},#{lidcard},#{lno},#{remark},"
			+ "#{description},#{exptime},now())")
	void saveLoanInfo(LoanInfo loanInfo);
	
	@Update("update p2p_loan_info set uid=#{uid}, title =#{title}, type =#{type},is_flow =#{isFlow},use_type=#{useType},amount =#{amount.cent},late_fee=#{lateFee.cent},"
			+ "fee_rate =#{feeRate},deadline =#{deadline},early_fee =#{earlyFee.cent},field_fee =#{fieldFee.cent},repay_method =#{repayMethod},"
			+ "tender_min =#{tenderMin.cent},mfee_rate =#{mfeeRate},credit_fee =#{creditFee.cent},other_fee =#{otherFee.cent},"
			+ "tender_max =#{tenderMax.cent}, apr =#{apr},credit_rate=#{creditRate},is_field =#{isField},is_plan =#{isPlan},is_okout =#{isOkout},"
			+ "deadline =#{deadline},deadline_type =#{deadlineType},tender_begin =#{tenderBegin},lrealname =#{lrealname},"
			+ "lidcard =#{lidcard},lno =#{lno},remark =#{remark},exptime=#{exptime},"
			+ "is_newbie =#{isNewbie},no_captcha =#{noCaptcha},plid = #{plid},reward_apr = #{rewardApr} where lid=#{lid}")
	void updateLoanInfo(LoanInfo loanInfo);
	
	List<LoanInfo> findByStatus(List<Integer> statusList);
	
	@Update("update p2p_loan_info set auto=#{auto} where lid=#{lid} and auto=#{oldAuto}")
	public int updateAuto(@Param(value="lid")Long lid, @Param(value="auto")Integer auto, @Param(value="oldAuto")Integer oldAuto);

	
	@Update("update p2p_loan_info set tender_end=#{tenderEnd},ostatus = #{ostatus} ,status=#{status} where lid=#{lid}")
	public int updateForTenderEnd(@Param(value="lid")Long lid,@Param(value="status")int status,@Param(value="ostatus")int ostatus,@Param(value="tenderEnd") Date tenderEnd);
	

	@Update("update p2p_loan_info set begintime=#{begintime},endtime = #{endtime} where lid=#{lid}")
	public int updateBeginEndTime(@Param(value="lid")Long lid, @Param(value="begintime")Date begintime,  @Param(value="endtime")Date endtime);

	@Select("select sum(amount) from p2p_loan_info where status > 32 and tender_end between #{dateBegin} and #{dateEnd}")
	Long getDayAmount(@Param("dateBegin")Date dateBegin, @Param("dateEnd")Date dateEnd);
	
	@Update("update p2p_loan_info set description=#{description} where lid=#{lid}")
	void updateDesc(@Param("lid")Long lid, @Param("description")String description);
	
	@Select("select * from p2p_loan_info where status in (12,21,32,41,51) order by status asc,tender_begin desc limit 0,4")
	@ResultMap("LoanInfoResultMap")  
	List<LoanInfo> getLastSucceededLoan();

	//满标待审-等待放款
	@Select("select * from p2p_loan_info where status = 32")
	List<Map<String, Object>> getAllWait();
	
	@Select("select * from p2p_loan_info where is_newbie > 0 limit 0,1")
	LoanInfo getTop();
}
